package com.datang.pet.common.ossUtils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.datang.pet.common.properties.OssAttr;
import org.springframework.stereotype.Component;

import java.io.InputStream;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-05-14 09:23
 * @notify 上传文件工具类
 * @version 1.0
 */
@Component
public class UploadFile {

    /*
     * @author 顶风少年
     * @date 2019/5/14 9:48
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param inputStream 文件输入流
     * @param fileName 文件名
     * @param type 1 图片 2 视频
     * @result boolean 成功或失败
     * @notify
     * version 1.0
     */
    public boolean uploadFile(InputStream inputStream, String fileName, Integer type) {

        //创建OSS实例
        OSSClient ossClient = new OSSClient(OssAttr.ENDPOINT, OssAttr.ACCESSKEYID, OssAttr.ACCESSKEYSECRET);

        try {

            //获取bucket名称
            String bucketName = OssUtil.getBucketName(type);

            //上传oss对象
            PutObjectResult putObjectResult = ossClient.putObject(bucketName, fileName, inputStream);

            //关闭连接
            ossClient.shutdown();

            return true;

        } catch (Exception e) {
            //关闭连接
            ossClient.shutdown();
            return false;
        }
    }
}
